<!--车辆管理页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">车辆管理</span>
    <el-button @click="openDialog('新建车辆')" type="primary" style="float:right;margin-top:13px;">新建车辆</el-button>
  </div>
  <!-- 新增车辆弹窗 -->
  <el-dialog @close="cancleDialog" :title="dialogTitle" v-model="dialogVisible" style="width:1000px;padding:40px;">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆品牌">
            <el-input v-model="vehicleModelForm.brand" placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆牌号">
            <el-input v-model="vehicleModelForm.license" placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆型号">
            <el-input v-model="vehicleModelForm.model" placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆识别码">
            <el-input v-model="vehicleModelForm.code" placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆排量">
            <el-select v-model="vehicleModelForm.displacement" placeholder="请选择">
              <el-option label="1.6" value="1"></el-option>
              <el-option label="2.5" value="2"></el-option>
              <el-option label="4" value="3"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆类型">
            <el-select v-model="vehicleModelForm.type" placeholder="请选择">
              <el-option label="轿车" value="10"></el-option>
              <el-option label="货车" value="20"></el-option>
              <el-option label="客车" value="30"></el-option>
              <el-option label="挂车" value="40"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车身颜色">
            <el-select v-model="vehicleModelForm.color" placeholder="请选择">
              <el-option label="黑" value="10"></el-option>
              <el-option label="白" value="20"></el-option>
              <el-option label="蓝" value="30"></el-option>
              <el-option label="灰" value="40"></el-option>
              <el-option label="银" value="50"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="里程数">
            <el-input v-model="vehicleModelForm.kilometers" placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="购买时间">
            <el-date-picker v-model="vehicleModelForm.buyTime" type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="上牌时间">
            <el-date-picker @change="validateDate" v-model="vehicleModelForm.regTime" type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="购买价格">
            <el-input v-model="vehicleModelForm.price" placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电池类型">
            <el-select v-model="vehicleModelForm.batteryType" placeholder="请选择">
              <el-option label="铅酸蓄电池" value="10"></el-option>
              <el-option label="镍氢电池" value="20"></el-option>
              <el-option label="钠硫电池" value="30"></el-option>
              <el-option label="二次锂电池" value="40"></el-option>
              <el-option label="空气电池" value="50"></el-option>
              <el-option label="三元锂电池" value="60"></el-option>
              <el-option label="碱性燃料电池" value="70"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="cancleDialog">取消</el-button>
      <el-button type="primary" @click="add">确定</el-button>
    </template>
  </el-dialog>
  <!-- 车辆搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="车辆品牌">
        <el-input v-model="searchVehicleForm.brand" @keydown.enter="getVehicleList" placeholder="请输入内容" style="width:220px;"></el-input>
      </el-form-item>
      <el-form-item label="车牌号" style="width:290px;">
        <el-input v-model="searchVehicleForm.license" @keydown.enter="getVehicleList" placeholder="请输入内容" style="width:220px;"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearchForm">重置</el-button>
        <el-button type="primary" @click="getVehicleList">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 车辆列表 -->
  <el-card style="margin:20px;">
    <el-table :data="vehicleArr">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="brand" label="车辆品牌" align="center"/>
      <el-table-column prop="license" label="车牌号" align="center"/>
      <el-table-column prop="code" label="车辆识别码" align="center"/>
      <el-table-column :formatter="vehicleTypeFormatter" prop="type" label="车辆类型" align="center"/>
      <el-table-column prop="price" label="购买价格" align="center"/>
      <el-table-column prop="buyTime" label="购买时间" align="center"/>
      <el-table-column prop="regTime" label="上牌时间" align="center"/>
      <el-table-column :formatter="batteryTypeFormatter" prop="batteryType" label="电池类型" align="center"/>
      <el-table-column :formatter="vehicleStatusFormatter" prop="status" label="车辆状态" align="center"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="editVehicleModel(scope.row.id)">编辑</el-button>
          <el-button link type="primary" size="small" @click="deleteVehicle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>
<script setup>
import {onMounted, ref, watchEffect} from "vue";
import qs from 'qs';
import axios from "axios";
import { ElMessage } from 'element-plus';
//控制新增车辆弹窗是否显示
const dialogVisible = ref(false);
//定义变量控制dialog的标题
const dialogTitle = ref('新增车辆');
//车辆列表
const vehicleArr = ref([]);
const vehicleModelForm = ref({
  brand:'',
  license:'',
  model:'',
  code:'',
  displacement:'',
  type:'',
  color:'',
  kilometers:'',
  buyTime:'',
  regTime:'',
  price:'',
  batteryType:''
});
const searchVehicleForm = ref({brand:'',license:''});


const getVehicleList =async ()=>{
  const {data:{data,code,msg}} = await axios.get(BASE_URL+"/v1/vehicle/select?"+qs.stringify(searchVehicleForm.value))
  if(code === 2000){
    vehicleArr.value = data;
  }else {
    ElMessage.error(msg);
  }
}

onMounted(()=>{
  getVehicleList();
  // loadDictOptions();
})

const resetSearchForm = ()=>{
  searchVehicleForm.value={};
  getVehicleList();
}

const cancleDialog = ()=>{
  dialogVisible.value = false;
  vehicleModelForm.value = {};
}
const openDialog = (title)=>{
  dialogVisible.value = true;
  dialogTitle.value = title;
}

const add =async ()=>{
 const {data:{code,data,msg}} =await axios.post(BASE_URL+"/v1/vehicle/save",qs.stringify(vehicleModelForm.value))
  if(code === 2000){
    ElMessage.success(vehicleModelForm.value.id? '修改成功!' : "添加成功!");
  }else{
    ElMessage.error(msg)
  }
  cancleDialog();
  getVehicleList();
}

const editVehicleModel =async (uid)=>{
  openDialog('编辑用户')
  const {data:{code,data,msg}} = await axios.get(BASE_URL + '/v1/vehicle/info/'+uid);
  vehicleModelForm.value = data;
}
const deleteVehicle = async uid=>{
  const { data:{data,code,msg} } = await axios.post(BASE_URL + "/v1/vehicle/delete/"+uid);
  if(code === 2000){
    ElMessage.success("删除成功!")
  }else{
    ElMessage.error(msg)
  }
  getVehicleList();
}

const vehicleTypeFormatter =(row,column,cellValue,index)=>{
  if(cellValue == 10){
    cellValue = '轿车';
  }else if(cellValue == 20){
    cellValue = '货车'
  }else if(cellValue == 30){
    cellValue = '客车'
  }else if(cellValue == 40){
    cellValue = '挂车'
  }
  return cellValue;
}

const batteryTypeFormatter = (row,column,cellValue)=>{
  if(cellValue == 10){
    cellValue = '铅酸蓄电池'
  }else if(cellValue == 20){
    cellValue = '镍氢电'
  }else if(cellValue == 30){
    cellValue = '钠硫电池'
  }else if(cellValue == 40){
    cellValue = '二次锂电池'
  }else if(cellValue == 50){
    cellValue = '空气电池'
  }else if(cellValue == 60){
    cellValue = '三元锂电池'
  }else if(cellValue == 70){
    cellValue = '碱性燃料电池'
  }

  return cellValue;
}

const vehicleStatusFormatter = (row,column,cellValue)=>{
  if(cellValue == 1){
    cellValue = '空闲';
  }else if(cellValue == 2){
    cellValue = '货车'
  }
  return cellValue;
}

const validateDate = ()=>{
  const buyTime = vehicleModelForm.value.buyTime;
  const regTime = vehicleModelForm.value.regTime;
  if(buyTime && regTime && new Date(regTime) <= new Date(buyTime)){
    ElMessage.error("上牌时间不能小于购买时间!")
    vehicleModelForm.value.regTime = '';
  }
}
</script>